package com.example.myblog_springboot.service.impl;

import cn.hutool.json.JSONObject;
import com.example.myblog_springboot.service.MoNiVannaTrainingDataService;
import org.springframework.stereotype.Service;

import java.util.*;

import static java.util.UUID.randomUUID;

@Service
public class MoNiVannaTrainingDataServiceImpl implements MoNiVannaTrainingDataService {

    private   static final List<Map<String,Object>> LOCAL_MAP = new ArrayList<>();


    @Override
    public JSONObject trainAdd(Map map) {
        String  ddl =(String) map.get("ddl");
        List<Map<String,Object>> localMap = LOCAL_MAP;
        String value = UUID.randomUUID().toString();
        if (ddl!=null){
            value = value +"-ddl";
           Map<String,Object> objectMap = new HashMap<>();
           objectMap.put("id",value);
           objectMap.put("ddl",ddl);
            localMap.add(objectMap);
        }else if (map.get("sql")!=null){
            value = value +"-sql";
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("id",value);
            objectMap.put("sql",map.get("sql"));
            localMap.add(objectMap);
        }else{
            value = value +"-document";
            Map<String,Object> objectMap = new HashMap<>();
            objectMap.put("id",value);
            objectMap.put("document",map.get("document"));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",value);
        System.out.println("数据库中的值"+LOCAL_MAP);
        return jsonObject;
    }

    @Override
    public boolean removeById(String id) {
        for (Map<String, Object> map : LOCAL_MAP) {
            if (map.get("id").equals(id)) {
                LOCAL_MAP.remove(map);
                System.out.println("数据库中的值"+LOCAL_MAP);
                return true;
            }
        }
        System.out.println("数据库中的值"+LOCAL_MAP);
        return false;
    }
}
